package com.mall.common.util;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;


/**
 * 数据库工具类
 * @author jzp
 *
 */
public class DBUtils {
	private static ThreadLocal<Connection> conns;
	/**
	 * 获取Connection连接对象
	 * @return
	 * @throws SQLException
	 */
	public static Connection getConnection() throws SQLException{
		DataSource dataSource = (DataSource) SpringUtils.getBean("dataSource");
		Connection conn = dataSource.getConnection();
		if(conns == null)
			conns = new ThreadLocal<Connection>();
		conns.set(conn);
		return conn;
	}
	
	/**
	 * 关闭数据库连接
	 * @throws SQLException
	 */
	public static void closeConnection() throws SQLException{
		Connection conn = conns.get();
		if(conn != null){
			conn.close();
		}
	}
}
